package com.foreknow.service.impl;

import com.foreknow.bean.Guestbook;
import com.foreknow.dao.impl.AdminDaoImpl;
import com.foreknow.dao.impl.GuestbookDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.service.GuestbookService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class GuestbookServiceImpl implements GuestbookService {
  @Override
  public List<Object> queryInfo() {
    //连接数据库
    DBManager dbManager = DBManager.getInstance();
    //调用连接数据库的方法
    Connection conn = dbManager.getConnection();
    //创建dao层的对象
    GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
    //调用AdminDaoImpl对象中的 public void setConnection(Connection connection)方法连接数据库
    guestbookDao.setConnection(conn);
    List<Object> list = null;
    try {
       list = guestbookDao.getQueryAll();
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return list;
  }

  @Override
  public boolean add(Guestbook gb) {
    //连接数据库
    DBManager dbManager = DBManager.getInstance();
    //调用连接数据库的方法
    Connection conn = dbManager.getConnection();
    //创建dao层的对象
    GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
    //调用AdminDaoImpl对象中的 public void setConnection(Connection connection)方法连接数据库
    guestbookDao.setConnection(conn);
    boolean bl = false;
    try {
      bl = guestbookDao.add(gb);
      conn.commit();
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return  bl;
  }

  @Override
  public boolean delete(Integer id) {
    //连接数据库
    DBManager dbManager = DBManager.getInstance();
    //调用连接数据库的方法
    Connection conn = dbManager.getConnection();
    //创建dao层的对象
    GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
    //调用AdminDaoImpl对象中的 public void setConnection(Connection connection)方法连接数据库
    guestbookDao.setConnection(conn);
    boolean dt = false;
    try {
      dt = guestbookDao.delete(id);
      conn.commit();
    } catch (SQLException throwables) {
      throwables.printStackTrace();
    }
    return dt;
  }
}
